home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / DOWNLOAD.CGI-S=COUNTER&C=TXT&F=HTML_LOG.PL < prev    next >
Perl Script  |  1996-06-03  |  10KB  |  289 lines

  1. #! /usr/local/bin/perl
  2. ##############################################################################
  3. # HTML Log                      Version 1.0                                  #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
  5. # Created 10/25/95              Last Modified 10/26/95                       #
  6. # Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
  7. # The file STAT_README contains more information. For Use With Counter 1.1.1 #
  8. ##############################################################################
  9. # COPYRIGHT NOTICE                                                           #
  10. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  11. #                                                                            #
  12. # HTML Log may be used and modified free of charge by anyone so long as      #
  13. # this copyright notice and the comments above remain intact.  By using this #
  14. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  15. # might arise from it's use.                                                 #  
  16. #                                                                            #
  17. # Selling the code for this program without prior written consent is         #
  18. # expressly forbidden.  In other words, please ask first before you try and  #
  19. # make money off of my program.                                              #
  20. #                                                                            #
  21. # Obtain permission before redistributing this software over the Internet or #
  22. # in any other medium.    In all cases copyright and header must remain intact.#
  23. ##############################################################################
  24. # Define Variables
  25.  
  26. $log_file = "/path/to/access_log";
  27.  
  28. $web = "1";
  29.  
  30. $min_refs = "5";
  31. $min_remote = "15"; 
  32. $min_agent = "5";
  33.  
  34. ##############################################################################
  35. # Select Options
  36.  
  37. $expand_agent = 0;      # 0 = NO; 1 = YES
  38. $show_percent = 1;      # 0 = NO; 1 = YES
  39.  
  40. $title = "Matt's Script Archive";
  41. $title_url = "http://worldwidemart.com/scripts/";
  42.  
  43. # Done
  44. ##############################################################################
  45.  
  46. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  47. if ($sec < 10)  { $sec = "0$sec";   }
  48. if ($min < 10)  { $min = "0$min";   }
  49. if ($hour < 10) { $hour = "0$hour"; }
  50. if ($mday < 10) { $mday = "0$mday"; }
  51. if ($mon < 10)  { $monc = "0$mon";  }
  52.  
  53. if ($web == 1) {
  54.    print "Content-type: text/html\n\n";
  55. }
  56.  
  57. $date_now = "$hour\:$min\:$sec $mon/$mday/$year";
  58.  
  59. open(DB,"$log_file") || die "Cannot Open Log File $log_file: $!";
  60. @lines = <DB>;
  61. close(DB);
  62.  
  63. $accesses = @lines;
  64.  
  65. if ($lines[1] =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  66.    $first_date = $1;
  67. }
  68. else {
  69.    $first_date = 0;
  70. }
  71.  
  72. if ($lines[($accesses - 1)] =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  73.    $last_date = $1;
  74. }
  75. else {
  76.    $last_date = 0;
  77. }
  78.  
  79. foreach $line (@lines) {
  80.    if ($line =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  81.       $date = $1;
  82.       ($clock,$time,$day) = split(/ /,$date);
  83.       ($hour,$minute,$second) = split(/:/,$clock);
  84.       $referer = $2;
  85.       $referer =~ s/\%24/\$/g;
  86.       $referer =~ s/\%7E/~/g;
  87.       $remote_host = $3;
  88.       $user_agent = $4;
  89.  
  90.       if ($time eq 'PM') {
  91.          $hour += 12;
  92.       }
  93.  
  94.       if ($day ne '' && $day ne ' ') {
  95.          push(@DAYS, $day);
  96.       }
  97.       if ($hour ne '' && $hour ne ' ') {
  98.          push(@HOURS, $hour);
  99.       }
  100.       if ($referer ne 'No Referer' && $referer ne ' ' && $referer ne '') {
  101.          push(@REFERER, $referer);
  102.       }
  103.       if ($remote_host ne 'No Remote_Host' && $remote_host ne ' ' && $remote_host ne '') {
  104.          push(@REMOTE_HOST, $remote_host);
  105.       }
  106.       if ($user_agent ne 'No User_Agent' && $user_agent ne ' ' && $user_agent ne '') {
  107.          push(@USER_AGENT, $user_agent);
  108.       }
  109.    }
  110. }
  111.  
  112. foreach (@REFERER) {
  113.    $refs{($_)[0]}++;
  114.    $i++;
  115. }
  116.  
  117. foreach (@REMOTE_HOST) {
  118.    $remote{($_)[0]}++;
  119.    $j++;
  120. }
  121.  
  122. foreach (@USER_AGENT) {
  123.    if ($expand_agent == 1) {
  124.       $agent{($_)[0]}++;
  125.    }
  126.    else {
  127.       $agent{(split('/',$_))[0]}++;
  128.    }
  129.    $k++;
  130. }
  131.  
  132. foreach (@DAYS) {
  133.    $day{($_)[0]}++;
  134. }
  135.  
  136. foreach (@HOURS) {
  137.    $hour{($_)[0]}++;
  138. }
  139.  
  140. &html_header;
  141.  
  142. sub html_header {
  143.    print "<html><head><title>Access Stats for $title</title></head>\n";
  144.    print "<body><center><h1>Access Stats for $title</h1></center>\n";
  145.    print "Below are the access stats for $title.<p>\n";
  146.    print "A total of $accesses were reviewed for this logging, which occurred at: $date_now<p>\n";
  147.    if ($first_date != 0 && $last_date != 0) {
  148.       print "These statistics reflect accesses from: <i>$first_date</i> to <i>$last_date</i><p>\n";
  149.    }
  150.    print "<p><hr size=7 width=75%>\n";
  151.    print "<font size=-1>[ <a href=\"\#refs\">Referring Web Pages</a> ] [ <a href=\"\#remote_host\">Remote Hosts</a> ] [ <a href=\"\#browsers\">Browsers</a> ] [ <a href=\"\#days\">Hits by Day</a> ] [ <a href=\"\#hours\">Hits by Hour</a> ] [ <a href=\"$titl
  152.  
  153.  
  154.  
  155.  
  156. e_url\">$title</a> ]</font>\n";
  157.    print "<hr size=7 width=75%><p>\n";
  158.  
  159.    &html_referer;
  160.    &html_remote_host;
  161.    &html_user_agent;
  162.    &days;
  163.    &hours;
  164. }
  165.  
  166. sub html_referer {
  167.    print "<center><h2><a name=\"refs\">Referring Web Pages</a></h2></center>\n";
  168.    print "<b>Referring URLs Searched:</b> <i><u>$i</u></i><br>\n";
  169.    print "<b>Minimum References Required to Make List:</b> $min_refs</u></i><p>\n";
  170.  
  171.    print "<table border width=100%>\n";
  172.    if ($show_percent == 1) {
  173.       print "<tr><th>Number </th><th>Percent </th><th>Referring Web Sites<br></th></tr>\n";
  174.    }
  175.    else {
  176.       print "<tr><th>Number </th><th>Referring Web Sites<br></th></tr>\n";
  177.    }
  178.    foreach (sort { $refs{$b} <=> $refs{$a} } keys %refs) {
  179.       if ($refs{$_} >= $min_refs) {
  180.          print "<tr>\n";
  181.          $total_refs += $refs{$_};
  182.          if ($show_percent == 1) {
  183.             $percent_refs = (int(10000 * ($refs{$_} / $i)) / 100);
  184.             $total_percent_refs += $percent_refs;
  185.             print "<th>$refs{$_} </th><th>$percent_refs\% </th><td><a href=\"$_\">$_</a><br></td>\n";
  186.          }
  187.          else {
  188.             print "<th>$refs{$_} </th><td><a href=\"$_\">$_</a><br></td>\n";
  189.          }
  190.          print "</tr>\n";
  191.       }
  192.    }
  193.    if ($show_percent == 1) {
  194.       print "<tr><th>$total_refs </th><th>$total_percent_refs\% </th><th>Totals For URLS Shown<br></th></tr>\n";
  195.    }
  196.    else {
  197.       print "<tr><th>$total_refs </th><th>Totals For URLS Shown<br></th></tr>\n";
  198.    }
  199.    print "</table><hr size=7 width=75%><p>\n";
  200. }
  201.  
  202. sub html_remote_host {
  203.    print "<center><h2><a name=\"remote_host\">Remote Hosts</a></h2></center>\n";
  204.    print "<b>Remote Hosts Searched:</b> <u><i>$j</i></u><br>\n";
  205.    print "<b>Minimum Hits Required to Make List:</b> <i><u>$min_remote</u></i><p>\n";
  206.  
  207.    print "<table border>\n";
  208.    if ($show_percent == 1) {
  209.       print "<tr><th>Number of Hits </th><th>Percent </th><th>Remote Hosts<br></th></tr>\n";
  210.    }
  211.    else {
  212.       print "<tr><th>Number of Hits </th><th>Remote Hosts<br></th></tr>\n";
  213.    }
  214.    foreach (sort { $remote{$b} <=> $remote{$a} } keys %remote) {
  215.       if ($remote{$_} >= $min_remote) {
  216.          print "<tr>\n";
  217.          if ($show_percent == 1) {
  218.             $percent_remote = (int(10000 * ($remote{$_} / $j)) / 100);
  219.             print "<th>$remote{$_} </th><th>$percent_remote\% </th><td>$_<br></td>\n";
  220.          }
  221.          else {
  222.             print "<th>$remote{$_} </th><td>$_<br></td>\n";
  223.          }
  224.          print "</tr>\n";
  225.       }
  226.    }
  227.    print "</table><hr size=7 width=75%><p>\n";
  228. }
  229.  
  230. sub html_user_agent {
  231.    print "<a name=\"browsers\"><center><h2>WWW Browsers</h2></center></a>\n";
  232.    print "<b>Browsers Searched:</b> <u><i>$k</i></u><br>\n";
  233.    print "<b>Minimum Hits Required to Make List:</b> <u><i>$min_agent</i></u><p>\n";
  234.  
  235.    print "<table border>\n";
  236.    if ($show_percent == 1) {
  237.       print "<tr><th>Number of Hits </th><th>Percent </th><th>Browser<br></th></tr>\n";
  238.    }
  239.    else {
  240.       print "<tr><th>Number of Hits </th><th>Browser<br></th></tr>\n";
  241.    }
  242.  
  243.    foreach (sort { $agent{$b} <=> $agent{$a} } keys %agent) {
  244.       if ($agent{$_} >= $min_agent) {
  245.          print "<tr>\n";
  246.          if ($show_percent == 1) {
  247.             $percent_agent = (int(10000 * ($agent{$_} / $k)) / 100);
  248.             print "<th>$agent{$_} </th><th>$percent_agent\% </th><td>$_<br></td>\n";
  249.          }
  250.          else {
  251.             print "<th>$agent{$_} </th><td>$_<br></td>\n";
  252.          }
  253.          print "</tr>\n";
  254.       }
  255.    }
  256.    print "</table>\n";
  257. }
  258.  
  259. sub hours {
  260.    print "<a name=\"hours\"><center><h2>Hits By Hour</h2></center></a>\n";
  261.    print "<table border>\n";
  262.    print "<tr><th>Hour </th><td>Number of Hits<br></td></tr>\n";
  263.    foreach (sort keys %hour) {
  264.       print "<tr>\n";
  265.       print "<th>$_ </th><td>$hour{$_}<br></td>\n";
  266.       print "</tr>\n";
  267.    }
  268.    print "</table>\n";
  269. }
  270.  
  271. sub days {
  272.    print "<a name=\"days\"><center><h2>Hits By Day</h2></center></a>\n";
  273.    print "<table border>\n";
  274.    print "<tr><th>Day </th><td>Number of Hits<br></td></tr>\n";
  275.    foreach (sort keys %day) {
  276.       print "<tr>\n";
  277.       print "<th>$_ </th><td>$day{$_}<br></td>\n";
  278.       print "</tr>\n";
  279.    }
  280.    print "</table>\n";
  281. }
  282.  
  283. sub html_trailer {
  284.    print "<p><hr size=7 width=75%>\n";
  285.    print "<font size=-1>[ <a href=\"\#refs\">Referring Web Pages</a> ] [ <a href=\"\#remote_host\">Remote Hosts</a> ] [ <a href=\"\#browsers\">Browsers</a> ] [ <a href=\"$title_url\">$title</a> ]</font>\n";
  286.    print "<hr size=7 width=75%><p>\n";
  287.    print "</body></html>\n";
  288. }
  289.